package com.webex.ta.hydra.gui.util;

import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.gui.table.HydraTable;
import com.webex.ta.hydra.util.HydraUtils;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-8
 * Time: 16:33:27
 */
public class Gutter extends Component implements MouseMotionListener, TableModelListener, MouseListener {
    private HydraTable table;

    private Vector<Integer> invalidLineNumbers = new Vector<Integer>();

    private Vector<Integer> yCoordinates = new Vector<Integer>();

    private int totalLines;

    private static final Color DARK = new Color(0xcc, 0xcc, 0xcc);

    public Gutter(HydraTable table) {
        super();
        this.table = table;
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
        table.getModel().addTableModelListener(this);
    }


    public Dimension getPreferredSize() {
        return new Dimension(13, 200);
    }

    @Override
    public void paint(Graphics g) {
        recalculate();
        Dimension size = this.getSize();
        Graphics2D   g2d   =   (Graphics2D)   g;
        g2d.setStroke(new BasicStroke(3));
        yCoordinates.clear();
        for (Integer i : invalidLineNumbers) {
            int y = size.height * i / totalLines;
            g2d.setColor(DARK);
            g2d.drawLine(0, y+1, size.width, y+1);
            g2d.setColor(Color.red);
            g2d.drawLine(0, y, size.width-3, y);
            yCoordinates.add(Integer.valueOf(y));
        }
    }

    private void recalculate() {
        Vector<Command> commands = table.getModel().getCommands();
        totalLines = commands.size();
        invalidLineNumbers.clear();
        for (int i = 0; i<commands.size(); i++) {
            Command cmd = commands.get(i);
            if (!cmd.isValid()) {
                if (HydraUtils.isController(cmd) &&
                        cmd.isExpanded() &&
                        cmd.getValidationInfos().size() <= 0)
                    continue;
                invalidLineNumbers.add(Integer.valueOf(i));
            }
        }
    }

    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    public void mouseDragged(MouseEvent e) {

    }

    public void mouseMoved(MouseEvent e) {
        int y = e.getY();
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        for (Integer yCoordinate : yCoordinates) {
            if (y >= yCoordinate -3  && y <= yCoordinate +3)
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            else if (y < yCoordinate - 3)
                break;
        }
    }

    public void tableChanged(TableModelEvent e) {
        this.repaint();
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {
        int y = e.getY();
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        for (int i = 0; i< yCoordinates.size(); i ++) {
            Integer yCoordinate = yCoordinates.get(i);
            if (y >= yCoordinate -3  && y <= yCoordinate +3) {
                Integer line = invalidLineNumbers.get(i);
                Rectangle rect = table.getCellRect(line, 0, false);
                table.scrollRectToVisible(rect);
                table.getSelectionModel().setSelectionInterval(line, line);
//                table.makeVisible(table.getModel().getCommandAt(line));
            } else if (y < yCoordinate - 3)
                break;
        }

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }
}
